import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";


interface ProductDetailState {
  loading: boolean;
  error: string | null;
  DetailList: any;
}

const initialState: ProductDetailState = {
  loading: true,
  error: null,
  DetailList: null,
};
// export const getProductDetail = createAsyncThunk(
//   "getproductDetail/productDetail",
//   async (touristRouteId: string, thunkAPI) => {
//     // setLoading(true);
//     thunkAPI.dispatch(productDetailSlice.actions.fetchStart());
//     try {
//       const { data } = await axios.get(
//         `http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`
//       );
//       // setProduct(data);
//       thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));
//       // setLoading(false);
//     } catch (error: any) {
//       thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));
//       // setError(error.message);
//       // setLoading(false);
//     }
//   }
// );
//promise
export const getProductDetail = createAsyncThunk(
  "productDetail/getProductDetail",
  async (touristRouteId: string, thunkAPI) => {
    const { data } = await axios.get(
      `http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`
    );
    return data;
  }
);

//
//       thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));
//       // setLoading(false);
//     } catch (error: any) {
//       thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));
//       // setError(error.message);
//       // setLoading(false);
//     }
//   }
// );

export const productDetailSlice = createSlice({
  name: "productDetail",
  initialState,
  extraReducers: {
    [getProductDetail.pending.type]: (state) => {
      // return {...state,loading:true}
      state.loading = true;
    },
    [getProductDetail.fulfilled.type]: (state, action) => {
      state.DetailList = action.payload;
      state.loading = false;
    },
    [getProductDetail.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      state.error = action.payload;
      state.loading = false;
    },
  },
  reducers: {},
});
